﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BuXiaChu.OrderDishes.Model;
using BuXiaChu.OrderDishes.BLL;

namespace BuXiaChu.OrderDishes.Web.ServiceStation.Modules
{
    public partial class OrderList : System.Web.UI.UserControl
    {
        public string OrderId
        {
            get;
            set;
        }

        public string Address
        {
            get;
            set;
        }

        public string CallerId
        {
            get;
            set;
        }

        public string AreaId
        {
            get;
            set;
        }

        public string OrderStatus
        {
            get;
            set;
        }

        public string DeliveryManId
        {
            get;
            set;
        }

        protected List<OrderInfoModel> pageDataList = null;
        protected Dictionary<long, List<OrderItemModel>> orderItemList = null;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            #region
            var sqlWhere = ConditionString();
            var pageCount = 0;
            pageDataList = OrderInfoManager.Search(sqlWhere, 0, 0, out pageCount);
            var orderIds = "";
            pageDataList.ForEach(
                    item =>
                    {
                        orderIds += item.OrderCode.ToString() + ",";
                    }
                );
            orderIds = orderIds.Trim(',');
            if (!string.IsNullOrEmpty(orderIds))
            {
                sqlWhere = " OrderInfoId in(" + orderIds + ")";
                orderItemList = OrderInfoManager.SearchOrderItem(sqlWhere, 0, 0, out pageCount);
            }
            else
            {
                orderItemList = new Dictionary<long, List<OrderItemModel>>();
            }
            #endregion
        }

        private string ConditionString()
        {
            var sqlWhere = string.Format(" OrderTime>= '{0}'", DateTime.Now.ToString("yyyy-MM-dd 00:00:00"));

            var orderId = 0;
            if (int.TryParse(OrderId, out orderId))
            {
                sqlWhere += " and orderCode = " + OrderId;

                return sqlWhere;
            }

            if (!string.IsNullOrEmpty(CallerId))
            {
                sqlWhere += " and MobileTelephone = '" + CallerId + "'";
            }

            if (!string.IsNullOrEmpty(Address))
            {
                sqlWhere += " and Address like'%" + Address + "%'";
            }

            if (!string.IsNullOrEmpty(OrderStatus) && OrderStatus != "-1")
            {
                sqlWhere += " and Status in(" + OrderStatus + ")";
            }

            return sqlWhere;
        }

        protected string GetRestaurantName(long orderId)
        {
            #region
            var result = "";
            if (orderItemList.ContainsKey(orderId))
            {
                var tmplId = 0;
                orderItemList[orderId].ForEach(
                        item =>
                        {
                            if (item.RestaurantId != tmplId)
                            {
                                result += item.RestaurantName + "  ";
                                tmplId = item.RestaurantId;
                            }
                        }
                    );
            }
            return result;
            #endregion
        }
    }
}